const path = require("path")
const fs = require("fs")

const ejs = require("ejs")

const compile = (templateName, data) => {
  const templatePosition = `../templates/${templateName}`
  const templatePath = path.resolve(__dirname, templatePosition)
  return new Promise((resolve, reject) => {
    ejs.renderFile(templatePath, {
      data
    }, {}, (err, result) => {
      if (err) return reject(err);
      resolve(result)
    })
  })

}
//递归创建文件夹
const createDirSync = (pathName) => {
  if (fs.existsSync(pathName)) return true
  if (createDirSync(path.dirname(pathName))) {
    fs.mkdirSync(pathName)
    return true
  }
}

const writeToFile = (path, content) => {
  return fs.promises.writeFile(path, content);
}

module.exports = {
  compile,
  writeToFile,
  createDirSync
}